$(function() {
    //首页进去时
    var e = 3,
         page = 1,
        last = false;
    $.ajax({
            url:'/client/get?page=1&e='+e,
            data:{},
            type:'GET',
            dateType:'json',
            success:function(res){
                //解析res
                var obj = eval("("+res+")");
                console.log(obj);
                if(obj.error == 0){
                    //判断是否加载完毕
                    if (obj.num == 0 || last) {
                        last = true;
                        layer.msg('加载完毕！');
                        return;
                    }
                    //加载 成功函数
                    toSuccess(obj,page,e);
                    page++;
                }else{
                    layer.msg(obj.msg);
                }
            },
            error:function(){
                layer.msg('error');
            }
        });
    //接收类别
    var type = '';
    var typeName = '';
    $('.category-image').click(function () {
        $('#waterfull ul').empty();
            type = $(this).find("input[id=type]").val();
            typeName = $(this).find("input[id=typeName]").val();
        page = 1;
        $.ajax({
            url:'/client/get?page=1&e='+e,
            data:{type:type,typeName:typeName},
            type:'GET',
            dateType:'json',
            success:function(res){
                //解析res
                var obj = eval("("+res+")");
                console.log(obj);
                if(obj.error == 0){
                    //加载 成功函数
                    toSuccess(obj,page,e);
                    page++;
                }else{
                    layer.msg(obj.msg);
                    last = true;
                }

            },
            error:function(){
                layer.msg('error');
            }
        });

    });

//从详情页进入分类
//     $('.totype').click(function(){
//         console.log(this);
//          type = $(this).find(".totype_one").text();
//          typeName = $(this).find(".totype_two").text();
//         // parent.location.href="/client/type/"+type;
//         $('#waterfull ul').empty();
//         alert(typeName);
//         page = 1;
//         $.ajax({
//             url:'/client/get?page=1&type='+type,
//             data:{type:type,typeName:typeName},
//             type:'GET',
//             dateType:'json',
//             success:function(res){
//                 //解析res
//                 var obj = eval("("+res+")");
//                 console.log(obj);
//                 if(obj.error == 0){
//                     //加载 成功函数
//                     toSuccess(obj,page,e);
//                     page++;
//                 }else{
//                     layer.msg(obj.msg);
//
//                 }
//                 last = true;
//             },
//             error:function(){
//                 layer.msg('error');
//             }
//         });
//     });
    /*****************************************************/
    $(window).scroll(function () {//alert(21);
        if(($(window).scrollTop() + $(window).height()) == $(document).height()){
            if (page > 1) {
                $.ajax({
                    type: "GET",
                    url: '/client/get?page='+page+'&e='+e,
                    data: {type:type,typeName:typeName},
                    async:false,
                    success: function(res) {
                        //解析res
                        var obj = eval("("+res+")");
                        console.log(obj);
                        if(obj.error == 0){
                            //判断是否加载完毕
                            if (obj.num == 0 || Math.ceil(obj.num/e) < page || last == true) {
                                layer.msg('没有更多作品！');
                                return;
                            }
                            //加载 成功函数
                            toSuccess(obj,page,e);
                            //页数超过总页  停止加载
                            page++;
                        }else{
                            layer.msg(obj.msg);
                        }
                    },
                    error: function() {
                        alert("network error");
                    }
                });
            }
        }
    });


});
    //ajax成功之后调用函数
    function toSuccess(obj,page,e){
    var boxes = new Array;
    var sqlJson = obj.list;
    var div = $('#waterfull ul');
    for(var i=0;i<sqlJson.length;i++){
        var html="";
        html+="<li class='item' style='display: none'><a onclick='opena("+sqlJson[i].id+")'  class='a-img'><img src='/productImg/"+sqlJson[i].picture+"'></a>";
        html+="<div class='choose'><div class='collect'><a onclick='setcollection("+sqlJson[i].id+","+page+","+i+")'><img src='/client/image/star.png''><p id='collect"+page+i+"'>"+sqlJson[i].num_c+"</p></a></div><div class='like'><a onclick='setlike("+sqlJson[i].id+","+page+","+i+")'><img src='/client/image/heart_b.png'><p class='num_love' id='like"+page+i+"'>"+sqlJson[i].num_love+"</p></a></div></div>";
        html+="<h2 class='li-title'>"+sqlJson[i].name+"</h2>";
        html+="<p class='description'>"+sqlJson[i].brief+"</p><div class='qianm clearfloat'>";
        html+="<div class='user-block'><div class='user' ><img style='border-radius:50%' src='/portraitImg/"+sqlJson[i].artist_portrait+"'></div></div><div class='user-introduction'>";
        html+="<div class='user-name'>"+sqlJson[i].artist_name+"</div><div class='user-skill'>"+sqlJson[i].artist_content+"</div> </div></div></li>";
        div.append(html);
        $('#waterfull ul li').eq((page-1)*e+i).fadeIn(i*2000);
    }
        boxes.push(div);
    $("#demo").gridalicious('append', boxes);
}

    function hotsearch(hot){
        e = 2;
        $('#waterfull ul').empty();
        $.ajax({
            url:'/client/get?page=1&e='+e,
            data:{search:hot},
            type:'GET',
            dateType:'json',
            success:function(res){
                //解析res
                var obj = eval("("+res+")");
                if(obj.error == 0){
                    console.log(obj);
                    //加载 成功函数
                    toSuccess(obj);
                }else{
                    layer.msg(obj.msg);
                }
            },
            error:function(){
                layer.msg('error');
            }
        });
    }
    //打开详情页
    function opena(_id){
        layer.open({type: 2,area: ['800px', '90%'], skin: 'layui-layer-rim', content: ['/client/productDetails/'+_id]});

    }
    //喜欢
    function setlike(_id,page,i){
        var url = '/client/like',
            data = {id:_id};
            $.ajax({
                url:url,
                data:data,
                type:'GET',
                dateType:'json',
                async:false,
                success:function(res){
                    var res = eval("("+res+")");
                    console.log(res);
                    if(res.error == 0){
                        $("#like"+page+i).text(res.num_love);
                    }else if(res.error == 1){
                        layer.open({title:'登录',type: 2,area: ['520px', '640px'],skin: 'layui-layer-rim',content: ['/client/toLogin', 'no']});
                    }
                    layer.msg(res.msg);
                },
                error:function(){
                    alert('ERROR!');
                }
            });
    }
    //收藏
    function setcollection(_id,page,i){
        var url = '/client/collection',
            data = {id:_id};
        $.ajax({
            url:url,
            data:data,
            type:'GET',
            dateType:'json',
            async:false,
            success:function(res){
                var res = eval("("+res+")");
                if(res.error == 0){
                    $("#collect"+page+i).text(res.num_c);
                }else if(res.error == 1){
                    layer.open({title:'登录',type: 2,area: ['520px', '640px'],skin: 'layui-layer-rim',content: ['/client/toLogin', 'no']});
                }
                layer.msg(res.msg);
            },
            error:function(){
                alert('ERROR!');
            }
        });
    }

function totype(type,typeName){
    $('#waterfull ul').empty();
    page = 1;
    $.ajax({
        url:'/client/get?page=1&e='+e,
        data:{type:type,typeName:typeName},
        type:'GET',
        dateType:'json',
        success:function(res){
            //解析res
            var obj = eval("("+res+")");
            console.log(obj);
            if(obj.error == 0){
                //加载 成功函数
                toSuccess(obj,page,e);
                page++;
            }else{
                layer.msg(obj.msg);

            }
            last = true;
        },
        error:function(){
            layer.msg('error');
        }
    });
}
